<?php
// +----------------------------------------------------------------------
// | 全局分类基类
// +----------------------------------------------------------------------
// | Author: 李乾坤 <261001642@qq.com>
// +----------------------------------------------------------------------
class CategoryAction extends CommonAction {
	function _initialize() {
		parent::_initialize();
		$this->module=str_replace('Category', '', MODULE_NAME);
		$this->module=str_replace('category', '', MODULE_NAME);
	}
	public function index()
    {
        //列表过滤器，生成查询Map对象
        $map = $this->_search($this->module);
        $map['module']=$this->module;
        if(method_exists($this,'_filter')) {
            $map=$this->_filter($map);
        }
		$Category        = M('Category');
        if(!empty($Category)) {
        	$this->_list($Category,$map);
        }
		$this->display();
    }	
	function _filter($map){
        $map['title']=array('like','%'.$_REQUEST['title'].'%');
        return $map;
    }
	function read() {
		$this->edit();
	}
	function edit() {
		$Category	=	M('Category');
		$id     = $_REQUEST['id'];
		$vo	=	$Category->find($id);
		$this->assign('vo',$vo);
		$this->display();
	}
    function _Before_Add(){
		$Category=M('Category');
		$list=$Category->where('status=1 and module="'.$this->module.'"')->getField('id,title');
		//echo $Category->getlastsql();
		$this->assign('CateList',$list);
	}
    function _Before_edit(){
		$this->_Before_Add();
	}
	function insert(){
		parent::insert('Category');
	}
	function update(){
		parent::update('Category');
	}
	function delete(){
		parent::delete('Category');
	}
	
	function __del($action=null,$pid='cid'){
		$ids= explode(',',$_REQUEST["id"]);
		if(empty($ids)){
			$this->error('请选择删除项!');
		}
		$action=is_null($action) ? $this->module : $action;
		$Category=M('Category');
		$ac1=M($action);
		$CateDelArr=array();
		$ContentDelArr=array();
		$newArr=array();
		foreach($ids as $id){
			$title=$Category->where('id='.$id)->getField('title');
			$tmp=$Category->where("cid={$id}")->count('id');
			if ($tmp>0){
				$CateDelArr[]=$title;
				continue;
			}
			$tmp=$ac1->where("{$pid}={$id}")->count('id');
			if ($tmp>0){
				$ContentDelArr[]=$title;
				continue;
			}
			$newArr[]=$id;
		}
		if(!empty($newArr)){
			$condition = array($pk=>array('in',$newArr));
			if(false === $model->where($condition)->delete()){
				$this->error(L('删除失败'));
			}
		}
		if(!empty($CateDelArr)){
			$msg.='<br/>'.implode(',',$CateDelArr).'下面还有子类';
		}
		if(!empty($CateDelArr)){
			$msg.='<br/>'.implode(',',$ContentDelArr).'下面还有内容';
		}
		if(!empty($msg)){
			$this->success('由于分类'.$msg.'<br/>所以无法删除');
		}else{
			$this->success('删除成功!');
		}
	}
    public function sort()
    {
		$model = M('Category');
        $map = array();
        $map['status'] = 1;
        $map['module'] = $this->module;
        if(!empty($_GET['sortId'])) {
            $map['id']   = array('in',$_GET['sortId']);
        }
        $sortList   =   $model->where($map)->Order('sort asc')->select();
        $this->assign("sortList",$sortList);
        $this->display('Public:sort');
        return ;
    }
}
?>